﻿package com.sh.game.control
{
    import com.core.*;
    import com.sh.game.consts.*;
    import com.sh.game.global.*;
    import com.sh.game.loader.*;
    import flash.display.*;
    import flash.events.*;
    import flash.system.*;
    import flash.utils.*;
    import morn.core.handlers.*;

    public class ModuleControl extends Control
    {
        protected var state:String = "close";
        protected var type:String = "startloadingpanel";
        protected var laters:Array;
        protected var module:Object;
        protected var moduleurl:String = "";
        protected var modulename:String = "";
        protected var callback:Function = null;
        protected var params:Object = null;
        protected var moduleInfo:Object;
        protected var isLoading:Boolean = false;
        protected var progress:Boolean = true;

        public function ModuleControl(param1:Object)
        {
            this.laters = [];
            super(param1);
            return;
        }// end function

        protected function callLater(param1:String, param2:String, param3:Object) : void
        {
            this.laters.push({o:param1, k:param2, v:param3});
            return;
        }// end function

        protected function apply() : void
        {
            var _loc_1:Object = null;
            var _loc_2:Obproxy = null;
            while (this.laters.length > 0)
            {
                
                _loc_1 = this.laters.shift();
                _loc_2 = ModelProxy.getProxy(_loc_1.o);
                this.update(_loc_2, new ProxyChange(_loc_1.k, null, _loc_1.v));
            }
            return;
        }// end function

        protected function open() : void
        {
            this.state = "open";
            if (this.module == null)
            {
                if (this.moduleInfo == null)
                {
                    this.loadModule(null);
                }
            }
            else if (this.module.parent)
            {
                this.module.visible = true;
            }
            else
            {
                this.module.visible = true;
                this.target.addChild(this.module);
            }
            return;
        }// end function

        protected function close() : void
        {
            this.state = "close";
            if (this.module)
            {
                this.module.visible = false;
            }
            return;
        }// end function

        public function loadModule(param1:Function, param2:Object = null) : void
        {
            if (this.type)
            {
                ModelProxy.change("loading", "state", this.type);
            }
            this.callback = param1;
            this.params = param2;
            this.isLoading = true;
            var _loc_3:* = SceneProloaderManager.instance.getCache(SceneProloaderManager.TYPE_SWF);
            if (_loc_3 && _loc_3[this.moduleurl])
            {
                this.completedHandler(_loc_3[this.moduleurl]);
            }
            else
            {
                SceneProloaderManager.instance.noNeed[this.moduleurl] = true;
                App.mloader3.loadBYTE(Config.getUrl(this.moduleurl, ResourceType.SWF), 0, new Handler(this.completedHandler), new Handler(this.usermoduleProgress), null, false);
            }
            return;
        }// end function

        protected function completedHandler(param1) : void
        {
            var _loc_4:String = null;
            var _loc_5:ByteArray = null;
            var _loc_6:int = 0;
            var _loc_7:ByteArray = null;
            var _loc_8:int = 0;
            if (param1 == null)
            {
                this.usermoduleError(null);
                return;
            }
            var _loc_2:* = param1;
            if (Config.Encrypt)
            {
                _loc_2.position = 0;
                _loc_4 = "8aff474e62f692ad1002b591a4b59794";
                _loc_5 = new ByteArray();
                _loc_5.writeMultiByte(_loc_4, "utf8");
                _loc_6 = _loc_2.length / 2;
                _loc_7 = new ByteArray();
                _loc_8 = 0;
                while (_loc_8 < _loc_5.length)
                {
                    
                    _loc_7.writeByte(_loc_2[_loc_8 + _loc_6] ^ _loc_5[_loc_8]);
                    _loc_8++;
                }
                _loc_2.position = _loc_6;
                _loc_2.writeBytes(_loc_7);
            }
            this.moduleInfo = new Loader();
            var _loc_3:* = new LoaderContext(false, ApplicationDomain.currentDomain);
            this.moduleInfo.contentLoaderInfo.addEventListener(Event.COMPLETE, this.loadSwfCompletedHandler);
            this.moduleInfo.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, this.usermoduleError);
            this.moduleInfo.loadBytes(_loc_2, _loc_3);
            _loc_2.clear();
            return;
        }// end function

        protected function usermoduleError(event:Event) : void
        {
            throw new Error("加载模块" + this.modulename + "出错");
        }// end function

        protected function usermoduleProgress(param1) : void
        {
            return;
        }// end function

        protected function setCenter() : void
        {
            this.module.x = this.target.width / 2 - this.module.width / 2;
            this.module.y = this.target.height / 2 - this.module.height / 2;
            return;
        }// end function

        protected function setTop() : void
        {
            if (this.module)
            {
                this.module.parent.setChildIndex(this.module as DisplayObject, (this.module.parent.numChildren - 1));
            }
            return;
        }// end function

        protected function loadSwfCompletedHandler(event:Event) : void
        {
            this.isLoading = false;
            event.currentTarget.removeEventListener(Event.COMPLETE, this.completedHandler);
            event.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, this.usermoduleError);
            this.module = event.currentTarget.content;
            this.initModule();
            if (this.callback != null)
            {
                this.callback(this.params);
            }
            if (this.moduleInfo is Loader)
            {
                (this.moduleInfo as Loader).unloadAndStop(false);
            }
            this.callback = null;
            this.params = null;
            this.moduleInfo = null;
            return;
        }// end function

        protected function initModule() : void
        {
            this.target.addChild(this.module);
            if (this.state == "open")
            {
                this.module.visible = true;
            }
            else
            {
                this.module.visible = false;
            }
            return;
        }// end function

    }
}
